package com.batchbrake.queue.actions;

import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import com.batchbrake.main.BatchBrake;
import com.batchbrake.main.EFile;
import com.batchbrake.queue.QueueBuilder;

public class AddFolderAction extends AbstractAction {
	private QueueBuilder builder = null;
	
	public AddFolderAction(QueueBuilder builder) {
		super("Add folder");
		this.builder = builder;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		JFileChooser dialog = new JFileChooser();
		FileNameExtensionFilter filter = new FileNameExtensionFilter("Movies", QueueBuilder.inputFormats);
		
		dialog.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		dialog.setDialogTitle("Select a folder to encode");
		dialog.setMultiSelectionEnabled(true);
		dialog.setFileFilter(filter);
		dialog.setSize(700, 500);
		
		int result = dialog.showOpenDialog(null);
		switch( result ) {
		case JFileChooser.APPROVE_OPTION:
			File[] files = dialog.getSelectedFiles();
			for( int i = 0; i < files.length; i++ ) {
				System.out.println(files[i].getParentFile());
				builder.addToQueue(new EFile(files[i].getParent()),
						BatchBrake.outputFolder, ".m4v");
			}
			new Thread(builder).start();
		case JFileChooser.CANCEL_OPTION:
		case JFileChooser.ERROR_OPTION:
			dialog.setVisible(false);
		}
	}
}
